package com.se2.antiqueexhibition.Mapper;

import com.se2.antiqueexhibition.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    @Insert("INSERT INTO t_comment (user_id, target_type, target_id, content, status) " +
            "VALUES (#{userId}, #{targetType}, #{targetId}, #{content}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Comment comment);

    @Select("SELECT * FROM t_comment WHERE id = #{id}")
    Comment selectById(Integer id);

    @Select("SELECT * FROM t_comment WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<Comment> selectByUserId(Integer userId);

    @Select("SELECT * FROM t_comment WHERE target_type = #{targetType} AND target_id = #{targetId} AND status = 1 ORDER BY create_time DESC")
    List<Comment> selectByTarget(@Param("targetType") String targetType, @Param("targetId") Integer targetId);

    @Select("SELECT * FROM t_comment WHERE status = #{status} ORDER BY create_time DESC")
    List<Comment> selectByStatus(Integer status);

    @Select("SELECT * FROM t_comment ORDER BY create_time DESC")
    List<Comment> selectAll();

    @Update("UPDATE t_comment SET content = #{content} WHERE id = #{id}")
    int update(Comment comment);

    @Update("UPDATE t_comment SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Integer status);

    @Delete("DELETE FROM t_comment WHERE id = #{id}")
    int deleteById(Integer id);

    @Delete("DELETE FROM t_comment WHERE user_id = #{userId}")
    int deleteByUserId(Integer userId);

    @Delete("DELETE FROM t_comment WHERE target_type = #{targetType} AND target_id = #{targetId}")
    int deleteByTarget(@Param("targetType") String targetType, @Param("targetId") Integer targetId);
}
